/***
 * @Author: LVGRAPE
 * @Date: 2023-09-20 18:42:54
 * @LastEditTime: 2023-09-20 18:47:32
 * @LastEditors: LVGRAPE
 * @Description:
 * @FilePath: \ZINO_FC_V4\ZINO\inc\rc.h
 * @要啥没啥，爱咋咋的
 */

#ifndef __RC_H_
#define __RC_H_
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus

#include "si24r1.h"
#include <stdbool.h>
#include "zino_config.h"
#define RTX_BUFFER_SIZE 20
#define RC_PAIRED_FLAG 0XBB
#define CRC_MAGICBEE 0XED

    enum BALL_RC_KEY_INDEX
    {
        BALL_ADJ_RUP = 0,
        BALL_ADJ_RDOWN,
        BALL_ADJ_RLEFT,
        BALL_ADJ_RRIGHT,
        BALL_ADJ_LLEFT,
        BALL_ADJ_LRIGHT,
        BALL_ADJ_LDOWN,
        BALL_ADJ_LUP,
        BALL_KEY_RT,
        BALL_KEY_RS,
        BALL_KEY_LS,
        BALL_KEY_LT,
    };
    enum FUNPLAY_RC_BUTTON_INDEX
    {
        FUNPLAY_KEY_PWR = 0,
        FUNPLAY_KEY_RT,
        FUNPLAY_KEY_RS,
        FUNPLAY_KEY_LS,
        FUNPLAY_KEY_LT,
    };


    enum
    {
        RC_STATE_IDLE,
        RC_STATE_PAIRING,
        RC_STATE_PAIR_FAILED,
        RC_STATE_PAIR_SUCCESS,
        RC_STATE_TRANSMITTING,
        RC_STATE_WAIT_ACK,
    };

    enum rc_mode
    {
        RC_MODE_NULL = 0,
        RC_MODE_PAIRING,
        RC_MODE_NORMAL,
    };

    enum wl_msg_head
    {
        WL_MSG_HEAD_NULL = 0,
        WL_MSG_HEAD_RC,          // rc 通道数据
        WL_MSG_HEAD_PAIR_REQ,    // 对频请求
        WL_MSG_HEAD_PAIR_COMFIR, // 对频确认
        WL_MSG_HEAD_FC_STATUS,   // 飞控状态

    };

    typedef enum
    {
        RC_BAT = 0,
        RC_RLR,
        RC_RUD,
        RC_LLR,
        RC_LUD,
        RC_KEY_VALUE,
        RC_MODE,
        RC_COUNT,
    } RC_STICK_BUTTOM_E;

    typedef union wl_msg
    {
        struct
        {
            uint8_t head;
            uint8_t index;
            uint16_t channel[8];
            uint8_t tail;
            uint8_t crc;
        } v;
        uint8_t raw[RTX_BUFFER_SIZE];
    } wl_msg_t;

    typedef struct rchandle
    {
        uint8_t address[5];
        uint8_t newAddress[5];
        uint8_t rfChannel;
        uint8_t newRfChannel;
        wl_msg_t tx_data;
        wl_msg_t rx_data;
        uint8_t crc;
        uint8_t rx_cnt;
        uint8_t tx_cnt;
        uint8_t state;
        uint8_t lost_cnt;
        uint8_t mode;
        uint8_t rssi;
        uint8_t pairTime;
        uint8_t pairOk;
        uint8_t pairConfirmTime;
    } rc_t;
    struct rc_config_list
    {
        uint8_t flag;
        uint8_t rfAddr[5];
        uint8_t rfChannel;
        uint8_t testFlag1;
        uint8_t testFlag2;
        uint8_t crc;
    };
    struct rc_pair_data_s
    {
        uint8_t flag;
        uint8_t address[5];
        uint8_t rf_channel;
    };
    int rc_task_init(void);
    rc_t *get_rc_channel_ram();
    uint16_t get_rc_channel(uint8_t rcchannel);
    void rc_into_trans_mode();
    void rc_into_pair_mode();
    void rc_into_normal_mode();
    void rc_keyActionHandle(uint8_t times);
    void rc_task_1ms(void);
    void rc_key_action_switch(void);
    void rc_task_1s(void);
    ZINO_CONFIG_DECLARE(struct rc_pair_data_s, pair_data);
    extern rc_t rchandle;
#ifdef __cplusplus
}
#endif // __cplusplus
#endif
